package com.zhangxun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhangxun.domain.UserCollection;
import com.zhangxun.service.UserCollectionService;
import com.zhangxun.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;

@RequestMapping("/p/collection")
@RestController
@Api(tags = "小程序用户收藏管理")
public class CollectionController {

    @Autowired
    private UserCollectionService userCollectionService;

    @GetMapping("/count")
    @ApiOperation("计算用户收藏数量接口")
    public ResponseEntity<Integer> count(){
        String userId = SecurityUtil.getWxUserId();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId,userId));
        return ResponseEntity.ok(count);
    }


    @GetMapping("isCollection")
    @ApiOperation("查询是否是收藏的商品接口")
    public ResponseEntity<Boolean> isCollection(Long prodId){

        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, SecurityUtil.getWxUserId()));
        return ResponseEntity.ok(count>0);
    }


    @PostMapping("/addOrCancel")
    @ApiOperation("添加或者取消收藏接口")
    public ResponseEntity<Boolean> addOrCancel(@RequestBody Long prodId){
       Boolean result = userCollectionService.addOrUpdateById(prodId);

        return ResponseEntity.ok(result);
    }
}
